<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
 <%@ page import="org.json.simple.JSONObject" %>
 <%@ page import="org.json.simple.parser.JSONParser" %>
 <%@ page import="org.json.simple.JSONArray" %>
 <%@ page import="com.netcracker.studportal.server.facade.*" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Admin View - Add Professor</title>
<link href="style/css/addStyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<%
	if ((session.getAttribute("username") == null) || ("").equals(session.getAttribute("username"))) {
		response.sendRedirect("login.jsp");
	}
	
	request.setCharacterEncoding("UTF-8");
	
	String resultMessage = null;
	FacadeAdmin fa = new FacadeAdmin();
	JSONParser parser = new JSONParser();
%>
<div class="header">
    <div class="wrapper">
        	<a href = "<% out.print("indexAdmin.jsp"); %>" >
        	<div class="logo">
        	<div class="exit"><a href = "<% out.print("logout.jsp"); %>" ><img src="style/img/exit.jpg"></a> </div> 
             <a href = "<% out.print("indexAdmin.jsp"); %>" ><img src="style/img/logo.png" class="log"></a>
            </a>
    </div>
</div>
    <div class="content">
    	<div class="wrapper">
        	<div class="left">
            	<ul class="top-menu">
                    <li><a href="<% out.print("admin.jsp?action=addStudent"); %>">Добавить нового студента</a></li>
                    <li><a href="<% out.print("admin.jsp?action=addProfessor"); %>">Добавить преподавателя</a></li>
                    <li><a href="<% out.print("admin.jsp?action=addExam"); %>">Добавить экзамен</a></li>
                    <li><a href="<% out.print("admin.jsp?action=AssignExam"); %>">Назначить экзамен</a></li>
                    
            	</ul>
            </div>
            <div class="right">
            	<div class="input_data">
                	<form method="post" action="AddProfessor.jsp?action=add">
			
			<input type="text" name="login"> Логин <br>
			<input type="password" name="password"> Пароль <br>
			<input type="text" name="first_name"> Имя <br>
			<input type="text" name="last_name"> Фамилия <br>
			<input type="text" name="patronymic"> Отчество <br>
			<input type="text" name="birthday"> День Рождения <br>
			
			<select name="univ_id" style="width: 155px; ">
			<option value=""></option>
				<%
					String university=fa.getAllUniversity();
					Object ob = parser.parse(university);
					JSONArray array = (JSONArray)ob;
					
					Iterator iter = array.iterator();
					
					while(iter.hasNext()){
					JSONObject data = (JSONObject)iter.next();
					String id = data.get("univ_id").toString();
					String univ_name = data.get("univ_name").toString();
					out.println("<option value=\""+id+"\">"+univ_name+"</option>");
					
					}
					
				%>
			</select> Университет <br>
			<input type="text" name="note"> Доп информация <br>
			<input type="submit" value="Добавить">
	</form>	
<%
	String login = null;
	String password = null;
	String f_name = null;
	String l_name = null;
	String patron = null;
	String birthday = null;
	String univ_id = null;
	String note = null;
	if ("add".equals(request.getParameter("action"))) 
	{
		if ((request.getParameter("login") == null || request.getParameter("login").isEmpty()) ||
			(request.getParameter("password") == null || request.getParameter("password").isEmpty()) ||
			(request.getParameter("first_name") == null || request.getParameter("first_name").isEmpty()) ||
			(request.getParameter("last_name") == null || request.getParameter("last_name").isEmpty()) ||
			(request.getParameter("patronymic") == null || request.getParameter("patronymic").isEmpty()) ||
			(request.getParameter("birthday") == null || request.getParameter("birthday").isEmpty()) ||
			(request.getParameter("univ_id") == null || request.getParameter("univ_id").isEmpty())
		   ) {
				out.println("<font color=red>Необходимо заполнить все поля!</font>");
			} else {
				login = request.getParameter("login");
				password = request.getParameter("password");
				f_name = request.getParameter("first_name");
				l_name = request.getParameter("last_name");
				patron = request.getParameter("patronymic");
				birthday = request.getParameter("birthday");
				univ_id = request.getParameter("univ_id");
				note = request.getParameter("note");
	
				JSONObject obj = new JSONObject();
				obj.put("login", login);
				obj.put("password", password);
				obj.put("first_name", f_name);
				obj.put("last_name", l_name);
				obj.put("patronymic", patron);
				obj.put("birthday", birthday);
				obj.put("univ_id", univ_id);
				obj.put("note", note);
	
				String param = obj.toString();
				String result = fa.addNewProfessor(param);
				JSONObject jsonObj = (JSONObject) parser.parse(result);
				if ("success".equals(jsonObj.get("status"))) {
					resultMessage = "<font color=green>Преподаватель успешно добавлен!</font>";
				} else {
					resultMessage = "<font color=red>При выполнении запроса возникли ошибки.</font>";
				}
			}
	}
%>
<%
  if (resultMessage != null) {
	  out.print("Результат: " + resultMessage);
  }
%>
                </div>
            </div>
            <div class="clear"></div>
        </div>
    </div>
    <div class="footer">
    	<div class="wrapper">
        	<div class="copy">
            	<p>Студенческий портал. 2014 &copy; by NetCracker</p>
            </div>
        </div>
    </div>	


</body>
</html>